/*
 *  Copyright 2019-2025 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.system.rest;

import cn.hutool.core.date.Week;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.logs.domain.BusinessLog;
import me.zhengjie.modules.logs.mapper.BusinessLogMapper;
import me.zhengjie.modules.logs.service.BusinessLogService;
import me.zhengjie.modules.system.vo.HistoryVo;
import me.zhengjie.modules.system.vo.WeekVo;
import me.zhengjie.tool.DateUtil;
import me.zhengjie.tool.WeekDateUtils;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.vo.Constant;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 首页展示数据接口
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/homePage")
public class HomePageController {
    @Resource
    private BusinessLogMapper businessLogMapper;


    @PostMapping
    public ResponseEntity<Object> createRole( @RequestBody Map resources){

        HashMap<Object, Object> resultmap = new HashMap<>();
        HashMap hashMap = new HashMap();
        hashMap.put("aae011", SecurityUtils.getCurrentUsername());
        List<BusinessLog> list = businessLogMapper.getList(hashMap);

        String yyyyMMdd = DateUtil.getCurrentTime("yyyyMMdd");
        String yyyyMM = DateUtil.getCurrentTime("yyyyMM");
        String yyyy = DateUtil.getCurrentTime("yyyy");

        List<BusinessLog> day = list.stream().filter(m -> yyyyMMdd.equals(m.getAae036().substring(0, 8))&& Constant.AAE016_1_SHTG.equals(m.getAae016())  ).collect(Collectors.toList());//当天办理量
        List<BusinessLog> year = list.stream().filter(m -> yyyyMM.equals(m.getAae036().substring(0, 6)) && Constant.AAE016_1_SHTG.equals(m.getAae016())  ).collect(Collectors.toList());//当天办理量
        List<BusinessLog> ht = list.stream().filter(m -> Constant.AAE016_4_YHT.equals(m.getAae016())  ).collect(Collectors.toList());//历史回退量


        //4个业务量数据获取
        resultmap.put("acount1",day.size());//当天
        resultmap.put("acount2",year.size());//当月
        resultmap.put("acount3",list.size());//历史
        resultmap.put("acount4",ht.size());//回退

        //当年业务办理情况数据

        ArrayList<Integer> expectedData = new ArrayList<>();

        for (int i = 1; i <= 12; i++) {//遍历获取1-12月的办理量
            String everyYear = yyyy+String.format("%02d", i);//获取月份  %d 表示整数  02 表示至少显示2位数字，不足2位时在前面补0
            List<BusinessLog> newyear = list.stream().filter(m -> everyYear.equals(m.getAae036().substring(0, 6)) && Constant.AAE016_1_SHTG.equals(m.getAae016())  ).collect(Collectors.toList());//当天办理量
            expectedData.add(newyear.size());
        }

//        expectedData.add(10);//1月
//        expectedData.add(8);
//        expectedData.add(20);
//        expectedData.add(6);
//        expectedData.add(15);
//        expectedData.add(19);
//        expectedData.add(21);
//        expectedData.add(3);
//        expectedData.add(0);
//        expectedData.add(0);
//        expectedData.add(0);
//        expectedData.add(0);

        //回退情况
        ArrayList<Integer> actualData = new ArrayList<>();

        for (int i = 1; i <= 12; i++) {//遍历获取1-12月的办理量
            String everyYear = yyyy+String.format("%02d", i);//获取月份  %d 表示整数  02 表示至少显示2位数字，不足2位时在前面补0
            List<BusinessLog> newyear = list.stream().filter(m -> everyYear.equals(m.getAae036().substring(0, 6)) && Constant.AAE016_4_YHT.equals(m.getAae016())  ).collect(Collectors.toList());//当天办理量
            actualData.add(newyear.size());
        }
//        actualData.add(4);
//        actualData.add(3);
//        actualData.add(5);
//        actualData.add(5);
//        actualData.add(6);
//        actualData.add(1);
//        actualData.add(2);
//        actualData.add(2);
//        actualData.add(0);
//        actualData.add(0);
//        actualData.add(0);
//        actualData.add(0);

        resultmap.put("expectedData",expectedData);//正向业务
        resultmap.put("actualData",actualData);//撤销数据


        //历史各个模块业务量获取

        ArrayList<Object> hisList = new ArrayList<>();
        hisList.add(new HistoryVo("基础业务",(int) list.stream().filter(m -> "01".equals(m.getAaa121().substring(0, 2))&& Constant.AAE016_1_SHTG.equals(m.getAae016())  ).count()));
        hisList.add(new HistoryVo("账户业务",(int) list.stream().filter(m -> "02".equals(m.getAaa121().substring(0, 2))&& Constant.AAE016_1_SHTG.equals(m.getAae016())  ).count()));
        hisList.add(new HistoryVo("退费业务",(int) list.stream().filter(m -> "03".equals(m.getAaa121().substring(0, 2))&& Constant.AAE016_1_SHTG.equals(m.getAae016())  ).count()));
        hisList.add(new HistoryVo("转出业务",(int) list.stream().filter(m -> "04".equals(m.getAaa121().substring(0, 2))&& Constant.AAE016_1_SHTG.equals(m.getAae016())  ).count()));
        hisList.add(new HistoryVo("支付业务",(int) list.stream().filter(m -> "05".equals(m.getAaa121().substring(0, 2))&& Constant.AAE016_1_SHTG.equals(m.getAae016())  ).count()));
        hisList.add(new HistoryVo("材料业务",(int) list.stream().filter(m -> "06".equals(m.getAaa121().substring(0, 2))&& Constant.AAE016_1_SHTG.equals(m.getAae016())  ).count()));

        resultmap.put("hisList",hisList);

        //本周各个模块办理情况统计


        // 获取本周日期列表
        List<String> weekDates = WeekDateUtils.getWeekDates(LocalDate.now());
        // 业务类型映射
        Map<String, String> businessTypes = new HashMap<>();
        businessTypes.put("basic", "01");    // 基础业务
        businessTypes.put("account", "02");  // 账户业务
        businessTypes.put("refund", "03");   // 退费业务
        businessTypes.put("transfer", "04"); // 转出业务
        businessTypes.put("finance", "05");  // 支付业务
        businessTypes.put("material", "06"); // 材料业务

        Map<String, List<Integer>> weekmap = new HashMap<>();

        // 为每个业务类型统计本周每天的数量
        for (Map.Entry<String, String> entry : businessTypes.entrySet()) {
            String businessKey = entry.getKey();
            String businessCode = entry.getValue();

            List<Integer> dailyCounts = new ArrayList<>();

            for (String weekDate : weekDates) {
                long count = list.stream()
                        .filter(m -> businessCode.equals(m.getAaa121().substring(0, 2))
                                && Constant.AAE016_1_SHTG.equals(m.getAae016())
                                && weekDate.equals(m.getAae036().substring(0, 8)))
                        .count();
                dailyCounts.add((int) count);
            }

            weekmap.put(businessKey, dailyCounts);
        }

        resultmap.put("week", weekmap);

        return new ResponseEntity<>(resultmap,HttpStatus.OK);
    }



}
